﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BookManageSystem
{
    class Dao
    {
        SqlConnection sc;//数据库连接对象
        public SqlConnection Concnect()//连接数据库
        {
            string str = @"Data Source=SEEKER\SQLEXPRESS;Initial Catalog=BookMS;Integrated Security=True;Encrypt=False;TrustServerCertificate=True";//数据库的连接串
            sc = new SqlConnection(str);
            sc.Open();
            return sc;
        }
        public SqlCommand Command(string sql)//执行一条sql语句
        {
            SqlCommand cmd = new SqlCommand(sql,Concnect());
            return cmd;
        }
        public int Execute(string sql)//获取执行sql语句后，数据库表中数据条数的更新数量
        {
            return Command(sql).ExecuteNonQuery();
        }
        public SqlDataReader read(string sql)//读取数据库中的数据
        {
            return Command(sql).ExecuteReader();
        }
        public void DaoClose()//关闭数据库
        {
            sc.Close();
        }
    }
}
